package org.example.service.impl;

import io.seata.spring.annotation.GlobalTransactional;
import org.example.dao.ResultDAO;
import org.example.feign.StudentApi;
import org.example.pojo.Result;
import org.example.service.IResulrService;
import org.example.service.IResultTccAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("ResultService")
public class ResultService implements IResulrService {
    private static final Logger logger = LoggerFactory.getLogger(ResultService.class);

    @Autowired
    private ResultDAO resultDAO;
    @Autowired
    private StudentApi studentApi;
    @Autowired
    private IResultTccAction resultTccAction;

    @Override
    @Transactional
    public void save(Result result) {
        resultDAO.save(result);
    }

    @Override
    @GlobalTransactional(name = "result-service")
    public void saveResult(Result result) {
        logger.info("分布式事务---开启");
        //result的保存开启事务
        resultTccAction.prepare(null, result.getId(), result.getScore());
        //question开启事务
        studentApi.updateStudentScore(result.getStudentid(), result.getScore());
        logger.info("分布式事务---结束");
    }

}
